<!DOCTYPE html>
<html lang="zh-cmn-Hans">
  <head>
    <meta charset="utf-8" />
    <title>单点登录</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <link rel="icon" href="../favicon.ico" />
    <script src="../static/config.js?t=001"></script>
    <script src="../js/js.cookie.min.js"></script>
    <script src="../js/common.js"></script>
    <script>
      const redirectUrl = 'redirect_url';
      let theCasParamNameTicket = '';
      function casLogin() {
        // 清除token
        setSSOType('casLogin', Cookies);
        const { casServerLoginUrl, casParamNameService, casParamNameTicket } = window.$config.casOption;
        theCasParamNameTicket = casParamNameTicket;
        if (getParameterFromURL(casParamNameTicket)) {
          let service = window.location.href;
          service = service.substring(0, service.indexOf(`${casParamNameTicket}=`));
          service = service.endsWith('&') ? service.substring(0, service.length - 1) : service;
          service = service.endsWith('?') ? service.substring(0, service.length - 1) : service;

          const param = {
            ticket: getParameterFromURL(casParamNameTicket),
            service: encodeURIComponent(service)
          };
          casAuth(param);
        } else {
          let service = window.location.href;
          // 校验
          if (!casParamNameService) {
            throw new Error(`无法完成Cas认证: 缺失回调地址参数配置${casParamNameService}`);
          }
          // 2.根据参数配置跳转
          window.location.href = `${casServerLoginUrl}?${casParamNameService}=${encodeURIComponent(service)}`;
        }
      }
      function casAuth(params) {
        try {
          // 给接口传的地址与adds服务的地址保持一致
          const href = encodeURIComponent(params.service);
          const param = `?ticket=${params.ticket}&service=${params.service}`;
          fetch(`/api/appsys/cas/loginByTicket${param}`, {
            method: 'get',
            headers: {
              'Content-Type': 'application/json'
            }
          })
            .then((response) => {
              if (!response.ok) {
                throw new Error('Network response was not ok ' + response.statusText);
              }
              return response.json(); // 解析JSON数据
            })
            .then((data) => {
              const { retCode, responseBody: result } = data;
              if (retCode === '200') {
                // 重定向优先级 redirectUrl > 后台用户
                if (window.location.href.indexOf(redirectUrl) > -1) {
                  storeData(result, Cookies);
                  let newPath = '';
                  const urlArr = decodeURIComponent(window.location.href.split(`${redirectUrl}=`)[1]);
                  if (urlArr.indexOf(theCasParamNameTicket) > -1) {
                    newPath = urlArr.substring(0, urlArr.indexOf(theCasParamNameTicket) - 1);
                  } else {
                    newPath = urlArr;
                  }
                  // const newPath = decodeURIComponent(window.location.href.split(`${redirectUrl}=`)[1]);
                  const url = window.location.href.substring(0, window.location.href.indexOf('/caslogin'));
                  window.name = 'portal';
                  window.location = `${url}${newPath}`;
                } else {
                  const { consoleUrl } = window.$config.consolePath;
                  if (result.userInfo && result.userInfo.consoleType === '0' && consoleUrl) {
                    consoleUrl = consoleUrl.endsWith('/') ? consoleUrl : `${consoleUrl}/`;
                    window.name = 'console';
                    window.open(`${consoleUrl}access-from-portal?access_token=${result.token}&redirect_url=/backstage`, 'console');
                  } else {
                    storeData(result, Cookies);
                    const url = window.location.href.substring(0, window.location.href.indexOf('/caslogin'));
                    window.name = 'portal';
                    window.location = url + '/dashboard';
                  }
                }
              }
            });
        } catch (error) {
          console.error(error);
        }
      }
      casLogin();
    </script>
  </head>

  <body id="app-body">
    <div id="app"></div>
  </body>
</html>
